package com.ykdl.member.kid.doctor;

import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.AskDoctorAdapter;
import com.ykdl.member.kid.beans.AskDoctorBean;
import com.ykdl.member.kid.beans.AskDoctorBeanListBean;
import com.ykdl.member.kid.beans.DoctorsBean;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;
/**
 * 医生在线Acivity
 * @author chenshuai
 */
public class DoctorOnlineActivity extends BaseScreen{
	private AskDoctorAdapter adapter ;
	private RefreshListView ask_doctor_list ;
	private View head = null;
	private AsyncImageView doctor_head;
	private TextView doctor_name,doctor_position,doctor_adrr,doctor_info;
	private String doctorid ="";
	private String name;
	private String TAG = "DoctorOnlineActivity";
	private boolean isfirst =true;
	private int start_id = 0;
	private int cursor = 0;
	private int count = 30; 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		doctorid  = this.getIntent().getStringExtra("doctor_id");
		name=this.getIntent().getStringExtra("doctor_name");
		setTopTitle("", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		
		setContent(R.layout.eventdetailactivity_xml);
		inint();
		
	}
	public void inint() {
		head = LayoutInflater.from(mContext).inflate(
				R.layout.eventdetail_head, null);
		adapter = new AskDoctorAdapter(this,false);
		doctor_head =  (AsyncImageView)head.findViewById(R.id.doctor_head);
		doctor_name =  (TextView)head.findViewById(R.id.doctor_name);
		doctor_position =  (TextView)head.findViewById(R.id.doctor_position);
		doctor_adrr =  (TextView)head.findViewById(R.id.doctor_adrr);
		doctor_info =  (TextView)head.findViewById(R.id.doctor_info);
		ask_doctor_list = (RefreshListView)findViewById(R.id.ask_doctor_list);
		ask_doctor_list.addHeaderView(head);
		doctor_info.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, DoctorInfoActivity.class);
				intent.putExtra("doctor_id", doctorid);
				intent.putExtra("doctor_name",name);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				mContext.startActivity(intent);
			}
		});
		findViewById(R.id.right_go).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, DoctorInfoActivity.class);
				intent.putExtra("doctor_id", doctorid);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				mContext.startActivity(intent);
			}
		});
		findViewById(R.id.ask_thedoctor).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				go(ToAskDoctorActivity.class);
			}
		});
		ask_doctor_list.setonRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				cursor = 0;
				inintdata(false);
			}});
		ask_doctor_list.setMoreClick(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				inintdata(false);
			}});
		ask_doctor_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if (arg2>1) {
					AskDoctorBean bean= (AskDoctorBean)adapter.getItem(arg2);
					Intent intent = new Intent(DoctorOnlineActivity.this,ProblemDetailsAcivity.class);
					intent.putExtra(KidAction.DOCTOR_TOPICI_DD, bean.getTopic_id()+"");
					DoctorOnlineActivity.this.startActivity(intent);
				}
				
			}
		});
		inintdata(true);
		
	}
	public void inintdata(boolean isshow) {
		if(isshow){
			showProgress("正在加载中,请稍等...");
		}
		String  pageurl =KidConfig.GET_DOCTOR_QUESTIONS_LIST;
		Map<String,String> map = new HashMap<String, String>();
		map.put("answer_actor_id", doctorid);
		map.put("acquire_actor_id", doctorid);
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,"");
		 TaskManager.startHttpRequest(request, new getDoctorQuestionsListTag(),  AskDoctorBeanListBean.class);
	}
	class getDoctorQuestionsListTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof AskDoctorBeanListBean) {
				AskDoctorBeanListBean bean = (AskDoctorBeanListBean)t;
				adapter.setList(bean.getList());
				ask_doctor_list.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				if (bean.getNext_cursor() == bean.getTotal_number()) {
					ask_doctor_list.setMoreButtoIsGon(true);
				} else {
					ask_doctor_list.setMoreButtoIsGon(false);
					cursor = bean.getNext_cursor();
				}
				if (isfirst&&null!=bean.getActor()) {
					setHeadData(bean.getActor());
				}
				isfirst = false ;
				ask_doctor_list.onRefreshComplete();
				ask_doctor_list.setFecthMoreOk();
				}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void setHeadData(UserActorBean bean) {
		if (bean.getAvatar_file_meta()!=null) {
			String url = bean.getAvatar_file_meta().getDownload_urls().getBig().getUrl();
			doctor_head.setUrl(TAG, url, ImageOptionsFactory.getSmallPortraitOptions(DoctorOnlineActivity.this));
		}
		doctor_name.setText(bean.getDisplay_name());
		setTopNav(bean.getDisplay_name());
		if (bean.getProfile()!=null) {
			doctor_position.setText(bean.getProfile().getTitle());
			doctor_adrr.setText(bean.getProfile().getDepartment());
			doctor_info.setText(bean.getProfile().getIntro());
		}
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
	
}
